package mysql

import (
	"ETikTok/models"
	"github.com/jmoiron/sqlx"
	"strings"
)

// CreateVideo 创建帖子
func CreateVideo(p *models.Video) (err error) {
	// sql语句
	sqlStr := `insert into video(video_id, author_id, tag_id, title, content,video_path) values(?,?,?,?,?,?)`
	// 执行
	_, err = db.Exec(sqlStr, p.ID, p.AuthorID, p.TagID, p.Title, p.Content, p.VideoPath)
	return
}

// GetVideoById 根据帖子ID查询帖子详情
func GetVideoById(pid int64) (video *models.Video, err error) {
	video = new(models.Video)
	// sql语句
	sqlStr := `select video_id, author_id, tag_id, title, content, video_path, create_time from video where video_id = ?`
	// 执行
	err = db.Get(video, sqlStr, pid)
	return
}

// GetVideoList 获取帖子列表
func GetVideoList(page, size int64) (videos []*models.Video, err error) {
	sqlStr := `select video_id, author_id, tag_id, title, content, video_path, create_time from video limit ?,?`

	videos = make([]*models.Video, 0, 2)
	err = db.Select(&videos, sqlStr, (page-1)*size, size)
	return
}

// GetVideoListByIDs 根据多个id查询帖子数据
func GetVideoListByIDs(ids []string) (videoList []*models.Video, err error) {
	sqlStr := `select video_id, author_id, tag_id, title, content, video_path, create_time
	from video
	where video_id in (?)
	order by FIND_IN_SET(video_id, ?)
	`
	query, args, err := sqlx.In(sqlStr, ids, strings.Join(ids, ","))
	if err != nil {
		return nil, err
	}
	query = db.Rebind(query)
	err = db.Select(&videoList, query, args...)
	return
}
